package com.hl.sfzy.controller.wxwappController;

import com.github.pagehelper.PageInfo;
import com.hl.sfzy.entity.wxwapp.ArticleDetail;
import com.hl.sfzy.entity.wxwapp.TeacherDetail;
import com.hl.sfzy.service.wxwappService.VueArticleService;
import com.hl.sfzy.utils.FileUpUtils;
import com.hl.sfzy.utils.R;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @className: VueArcitleController
 * @author: WXW
 * @date: 2024/9/9 10:08
 * @Version: 1.0
 * @description:
 */
@RestController
@RequestMapping("/article")
public class VueArticleController {

    @Autowired
    private VueArticleService vueArticleService;

    @GetMapping("/SelectAllArticle")
    public R selectAllArticle(Integer pageNum,Integer pageSize){
        PageInfo<ArticleDetail> allArticle = vueArticleService.findAllArticle(pageNum,pageSize);
        return R.success(allArticle);
    }
    @GetMapping("/SelectArticleById")
    public R selectAllTeacgerId(){
        List<TeacherDetail> allTeacherId = vueArticleService.findAllTeacherId();
        return R.success(allTeacherId);
    }
    //新增
    @PostMapping("/VueAddArticle")
    public R addArticle(@RequestBody ArticleDetail articleDetail){
        System.out.println("articleDetail = " + articleDetail);
        return vueArticleService.addArticle(articleDetail);
    }



//    @PostMapping("/fileupload")
//    public R<String> updateArticle(@RequestParam("file") MultipartFile file) throws IOException {
//
//        String filename = FileUpUtils.fileUp(file, "D:\\upload\\");
//        return R.success(filename);
//    }

    private String realPath = "d:/upload";
    @PostMapping("/fileupload")
    public Object upload( MultipartFile file)
            throws IOException {
        if(file.isEmpty()){ // 没有上传
            return R.failure("文件为空");
        }
        // 上传 4. 定义上传相关函数

        // 获取文件名
        String filename =
                file.getOriginalFilename();
        // 处理重名问题
        filename =
                UUID.randomUUID().toString()+filename;
        // 获取当前项目的upload文件夹
        // 处理日期作为文件夹问题
        SimpleDateFormat sdf = new  SimpleDateFormat("yyyyMM");

        String dname = sdf.format(new Date());

        // 判断文件夹是否存在
        File dfile = new
                File(realPath,dname);
        if(!dfile.exists()){
            dfile.mkdirs();
        }
        // 上传
        file.transferTo(new File(dfile,filename));

        // 组成上传的文件夹名和文件名 ，返回给前端
        filename = "/"+dname+"/"+filename;
        return R.success(filename);
    }


    @RequestMapping("/yuLan")
    public void yuLan(String filename,
                      HttpServletResponse response) throws
            IOException {
// 获取文件所属的真实路径
        File file = new
                File(realPath,filename);
// 组成字节输出流   vue的编辑中定义图片预览

        FileInputStream fin = new
                FileInputStream(file);
// 设置响应头信息
        response.setHeader("ContentDisposition", "filename="+filename);//预览
// 组成相应流
        ServletOutputStream out =
                response.getOutputStream();
// 拷贝
        IOUtils.copy(fin,out);
// 释放资源
        out.close();
        fin.close();
    }
}
